<?php
/**
 * 系统消息
 * Created by PhpStorm.
 * User: andy
 * Date: 2020/2/14
 * Time: 下午6:42
 */
namespace app\common\model;

use think\Model;

class SystemMsgModel extends Model
{
    const SHOW_STATUS=1; //正常
    const HIDE_STATUS=0; //无效

    const READ_NO=0; //未读
    const READ_OK=1; //已读

    protected $autoWriteTimestamp = true;

    public function user()
    {
        return $this->hasOne('MemberModel','uid','send_user_id')->field('uid,mobile,alipay_user,wechat_user');
    }

    /**
     * 所有推荐
     * @param array $where
     * @param int $pageSum
     * @return \think\Paginator
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getALL($where=[],$pageSum=0)
    {
        if($pageSum<=0){
            $pageSum=getSystem("pageSUM")?getSystem("pageSUM"):10;
        }

        $model=self::with('user');

            if(isset($where['send_user_id'])){
                $model->where('send_user_id','in',[$where['send_user_id'],'0']);
            }else{
                $model->where($where);
            }

        $list=$model->order('read_status asc,create_time desc')
            ->paginate($pageSum);


        return $list;
    }


    /**
     * 删除
     * @param $id
     * @return array
     */
    public function del($id){

        $status=self::destroy($id);
        if($status){
            return ['status'=>1,'msg'=>'删除成功！'];
        }else{
            return ['status'=>0,'msg'=>'删除失败！'];
        }
    }

    /**
     * 增加
     * @param $data
     * @return array
     */
    public static function adds($data){
        if(!isset($data['title'])) $data['title']="系统消息";

        $status = self::insertGetId($data);
        //更新发布车辆最新回复时间

        if($status){
            return['status'=>1,'msg'=>'增加成功！'];
        }else{
            return['status'=>0,'msg'=>'增加失败！'];
        }
    }

} 